<?php

class Reset extends Controller {
	
	function Reset()
	{
		//Constructor
		parent::Controller();
		$this->load->model('usermodel');
		$this->load->library('form_validation');
	}
	
	function index()
	{
		$this->form_validation->set_rules('email', 'Email', 'trim|required|xss_clean');
		$this->form_validation->set_rules('key', 'Activation Key', 'trim|required|xss_clean');
		$this->form_validation->set_rules('agree', 'I agree', 'trim|required|xss_clean');

		if($this->form_validation->run() == FALSE)
		{
			$data['titlebar'] = "Panoramap | Reset Password";
			$this->load->view('reset_pwd_view', $data);
		}
		else
		{
			$email = $this->input->post('email');
			$key = $this->input->post('key');
			if($this->usermodel->checkUserExists($email) == TRUE)
			{
				$this->session->set_flashdata('error',  'ไม่พบอีเมล์นี้อยู่ในระบบ กรุณาระบุอีเมล์ของคุณเท่านั้น!');
				redirect('reset');
			}
			elseif($this->usermodel->checkActivationKey($key, $email) == FALSE)
			{
				$this->session->set_flashdata('error',  'Activation Key this invalid!');
				redirect('reset');
			}
			else
			{
				$this->usermodel->resetPwd($email);
			}
		}
		
	}
	
}
